package testdb;

import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public abstract class HypersonicBasedDbTest extends DbTest {

    private String connectionString;

    public HypersonicBasedDbTest(PrintWriter writer, Record record, String connectionString) {
        super(writer, record);
        this.connectionString = connectionString;
    }

    @Override
    protected void insertTest() throws Exception {
        String sql = String.format("INSERT INTO PUBLIC.TEST_TABLE (COL1, COL2, COL3, COL4, COL5)"
                + " VALUES ('%s', '%s', '%s', '%s', '%s');",
                record.getCol1(), record.getCol2(), record.getCol3(),
                record.getCol4(), record.getCol5());

        int count = record.getCount();
        for (int j = 0; j < count; j++) {
            connection.createStatement().execute(sql);
        }
    }

    @Override
    protected void selectTest() throws Exception {
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery("SELECT * FROM PUBLIC.TEST_TABLE;");
    }

    @Override
    protected String connect() throws Exception {
        connection = DriverManager.getConnection(connectionString, "SA", "");
        return connectionString;
    }

    @Override
    protected void deleteAllData() throws Exception {
        String sql = "delete from PUBLIC.TEST_TABLE;";
        connection.createStatement().execute(sql);
    }

    @Override
    protected String getConnectionString() {
        return connectionString;
    }
}
